<?php
// +----------------------------------------------------------------------
// | TwoThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.twothink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 小矮人  82550565@qq.com <www.twothink.cn>
// +----------------------------------------------------------------------
namespace addons\adver\model;

use app\common\model\AddonsBase;
use think\Validate;
use think\Db;
/**
 * 分类模型
 */
class Adver extends AddonsBase{
    public $model_info = [
        'name' => 'adver',
        'pk' =>'id',
        'button' => [
            ['title'=>'新增','url'=>'edit?name=adver','icon'=>'','class'=>'ajax-get iframe bg-aqua','ExtraHTML'=>''],
            ['title'=>'删除','url'=>'del?name=adver','icon'=>'','class'=>'btn-danger ajax-post confirm','ExtraHTML'=>'target-form="ids"']
        ],
        //特殊字符串替换用于列表定义解析
        'replace_string' => [
            ['[DELETE]','[EDIT]','[ADDON]','[ADDONSETUS]','[ADVERTISING]'],
            ['del?ids=[id]&name=[ADDON]','edit?id=[id]&name=[ADDON]','adver','admin/addons/execute?_addons=adver&_controller=adver&_action=setstatus&ids=[id]',
                'admin/addons/execute?_addons=adver&_controller=advertising&_action=index&adver_id=[id]']],
        'field_group'=>'1:基础',
        "fields"=>[
            '1'=>[
                ['name'=>'id','title'=>'UID','type'=>'string','remark'=>'','is_show'=>4],
                ['name'=>'title','title'=>'位置名称','type'=>'string','remark'=>'请输入广告显示的位置','is_show'=>1],
                ['name'=>'type','title'=>'广告类型','type'=>'radio','extra'=>'1:单图;2:多图;3:文字;4:代码','remark'=>'请选择广告类型','value'=>1,'is_show'=>1],
                ['name'=>'width','title'=>'广告位宽度','type'=>'string','remark'=>'前端展示的宽度(100px auto 100%)','is_show'=>1],
                ['name'=>'height','title'=>'广告位高度','type'=>'string','remark'=>'前端展示的高度(100px auto 100%)','is_show'=>1],
                ['name'=>'status','title'=>'状态','type'=>'radio','extra'=>'0:禁用;1:启用','value'=>1,'remark'=>'状态','is_show'=>1],
            ]
        ],
        'list_grid' => [        //这里定义的是除了id序号外的表格里字段显示的表头名和模型一样支持函数和链接
            'id:编号',
            'title:广告位名称',
            'type:广告位类型',
            'width:广告位宽度',
            'height:广告位高度',
            'status:位置状态',
            'id:操作:[EDIT]|编辑,[status]|{0.启用.[ADDONSETUS]&status=1 1.禁用.[ADDONSETUS]&status=0},[ADVERTISING]|广告详情,[DELETE]|删除'
        ]
    ];
    public function editData($data = false, $id = '')
    {   $data = Request()->param();
        $rule = [
            ['title','require','位置名称必须']
        ];
        $validate = new Validate($rule);
        if (!$validate->check($data)) {
            $this->error = $validate->getError();
            return false;
        }
        return parent::editData($data, $id); // TODO: Change the autogenerated stub
    }
    /*  展示数据  */
    public function AdverList($param){
        $where['adver_id'] =  (int)$param;
        $where['start_time'] = ['lt',time()];
        $where['end_time'] = ['gt',time()];

        $result = Db::view('advsr_details', 'id,title,adver_id,img_id,advstext,advshtml,status')
            ->view('picture', ['path'],'picture.id = advsr_details.img_id')
            ->view('adver', ['type,width,height'],'adver.id = advsr_details.adver_id')
            ->where('adver.status', 'eq', 1)
            ->where('advsr_details.status', 'eq', 1)
            ->where($where)
            ->order('level asc,id asc')
            ->select();
        return $result;
    }
}